﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QUT.Bio.Util;
using QUT.Bio.SilverMap.ObjectModel;
using QUT.Bio.SilverMap.DefaultProvider;

namespace QUT.Bio.SilverMap.DefaultProvider {
	public partial class SequenceChooser : UserControl {
		private PopupDialog dialog = new PopupDialog();
		private ObjectModel model;

		public SequenceChooser ( ObjectModel model ) {
			InitializeComponent();

			this.model = model;
			
			dialog.Title = "Select sequences to search";
			dialog.ClientArea.Centre = this;
			dialog.WindowChanged += WindowChanged;

			if ( model.chromosomes.Count == 0 ) {
				model.ChromosomesLoaded += Refresh;
			}
			else {
				Refresh();
			}
		}

		public PopupDialog Dialog {
			get {
				return dialog;
			}
		}

		public Action WhenOkClicked {
			get;
			set;
		}

		public IEnumerable<Chromosome> SelectedChromosomes {
			get {
				return from item in selector.SelectedItems select (Chromosome) item;
			}
			set {
				selector.SelectedItems = from item in value select (ISelectable) item;
			}
		}

		private void Refresh() {
			List<Chromosome> allItems = new List<Chromosome>( model.Chromosomes.Values );
			allItems.Sort();
			selector.AllItems = from item in allItems
								select (ISelectable) item;
		}

		private void WindowChanged( PopupWindow window, WindowChangeEventType eventType ) {
			if ( eventType == WindowChangeEventType.WindowClosed ) {
				if ( dialog.Action == DialogAction.Ok ) {
					if ( WhenOkClicked != null ) {
						WhenOkClicked();
					}
				}
			}
		}
	}
}
